<?
require_once('ConverterBase.php');

class NumberConverter extends ConverterBase {
	const NUMBER= 0,CURRENCY= 1,PERCENT= 2;
	private $currencySymbol=null,$currencyCode=null;
	private $integerOnly= false,$type= self::NUMBER;

	// ACCESSORS
	public function setCurrencySymbol($value) { $this->currencySymbol= $value; return $this; }
	public function getCurrencySymbol() { return $this->currencySymbol; }

	public function setCurrencyCode($value) { $this->currencyCode= $value; return $this; }
	public function getCurrencyCode() { return $this->currencyCode; }

	public function setIntegerOnly($value) { $this->integerOnly= $value ? true : false; return $this; }
	public function getIntegerOnly() { return $this->integerOnly; }

	public function setType($value) {
		switch ($value) {
			case 'currency': $this->type= self::CURRENCY; break;
			case 'percent': $this->type= self::PERCENT; break;
			default: $this->type= self::NUMBER;
		}
		return $this;
	}
	public function getType() { return $this->type; }

	// METHODS
	protected function toDatatype($string) {
		if (trim($string)=== '') return null;
		else if (ereg_replace("[^0-9]",'',$string)=== '')
			throw new ConverterException($this,'unable_to_convert_empty_string',$string);
		if ($this->getType()=== self::CURRENCY) {
			$string= str_replace(array(
				$this->getCurrencySymbol(),
				$this->getCurrencyCode()
			),'',$string);
		}
		if ($this->getIntegerOnly())
			return (int)$string;
		else
			return (float)$string;
	}
	protected function toString($number) {
		$locale = localeconv();
		// Sign specifications
		if ($number> 0) {
			$sign = $locale['positive_sign'];
			$sign_posn = $locale['p_sign_posn'];
			$sep_by_space = $locale['p_sep_by_space'];
			$cs_precedes = $locale['p_cs_precedes'];
		} else {
			$sign = $locale['negative_sign'];
			$sign_posn = $locale['n_sign_posn'];
			$sep_by_space = $locale['n_sep_by_space'];
			$cs_precedes = $locale['n_cs_precedes'];
		}
		if ($this->getIntegerOnly()) $frac_digits= 0;
		else $frac_digits= $locale['frac_digits'];

		$type= $this->getType();
		// Currency format
		if ($type=== self::CURRENCY) {
			// given currency properties override locale
			if ($this->getCurrencySymbol()!==null || $this->getCurrencyCode()!==null) {
				// overwrite symbol
				if ($this->getCurrencySymbol())
					$currency= $this->getCurrencySymbol();
				// if no symbol is given overwrite with code and append as opposed to preced
				else {
					$currency= $this->getCurrencyCode();
					$cs_precedes= 0;
				}
			}
			// use locale
			else
				$currency= $locale['currency_symbol'];
			$number= number_format(
				$number,
				$frac_digits,
				$locale['mon_decimal_point'],
				$locale['mon_thousands_sep']
			);
			$space = $sep_by_space ? ' ' : '';
			if ($cs_precedes) $number = "$currency$space$number";
			else $number.= "$space$currency";
		}
		// Number format
		else {
			$number= number_format(
				$number,
				$frac_digits,
				$locale['decimal_point'],
				$locale['thousands_sep']
			);
		}

		// position sign
		switch($sign_posn) {
			case 0: $n = "($number)"; break;
			case 2:
			case 4: $n = "$number$sign"; break;
			case 1:
			case 3:
			default: $n = "$sign$number"; break;
		}
		return $number.($type=== self::PERCENT ? '%' : '');
	}
}
